package cc.avatar.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name="t_warehousing") 
public class Warehousing extends AccountingEvent{
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Long id;
	
	private String trackingNumber;
	
	@ManyToOne(cascade = {CascadeType.ALL})
	private Party maker;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(updatable = false)
	private Date whenOccurred;
	
	@OneToMany(cascade=CascadeType.ALL)
	@JoinColumn(name="warehousing_id")
	private Set<WarehousingItem> items = new HashSet<WarehousingItem>();
	
	//该批次产品需存储一定天数后方可销售
	private int usageDelayDays = 0;
	
	public Set<WarehousingItem> getItems() {
		return items;
	}

	public void setItems(Set<WarehousingItem> items) {
		this.items = items;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Party getMaker() {
		return maker;
	}

	public void setMaker(Party maker) {
		this.maker = maker;
	}
	@Override
	public Date getWhenOccurred() {
		return whenOccurred;
	}

	public void setWhenOccurred(Date whenOccurred) {
		this.whenOccurred = whenOccurred;
	}

	public int getUsageDelayDays() {
		return usageDelayDays;
	}

	public void setUsageDelayDays(int usageDelayDays) {
		this.usageDelayDays = usageDelayDays;
	}

	public String getTrackingNumber() {
		return trackingNumber;
	}

	public void setTrackingNumber(String trackingNumber) {
		this.trackingNumber = trackingNumber;
	}

	@Override
	public Party getStakeholder() {
		return this.maker;
	}
	
	public void addItem(WarehousingItem item){
		this.items.add(item);
	}
	@Transient
	public int getQuantityOfColumn(){
		int sum = 0;
		for (WarehousingItem item: this.getItems()){
			sum += item.getQuantityOfColumn();
		}
		return sum;
	}
	@Transient
	public int getQuantityOfBlock(){
		int sum = 0;
		for (WarehousingItem item: this.getItems()){
			sum += item.getQuantityOfBlock();
		}
		return sum;
	}
	
	@Override
	public String toString() {
		return this.getTrackingNumber()
	     + " 总条数:" + getQuantityOfColumn()
	     + " 总块数:" + getQuantityOfBlock();
	}

	@Override
	public String getAtt1() {
		return this.trackingNumber;
	}

	@Override
	public String getAtt2() {
		return String.valueOf(this.getQuantityOfColumn());
	}

	@Override
	public String getAtt3() {
		return String.valueOf(this.getQuantityOfBlock());
	}    
}
